import { Component, ElementRef, EventEmitter, Injector, Output, ViewChild } from '@angular/core';
import { AppConsts } from '@shared/AppConsts';
import { AppComponentBase } from '@shared/common/app-component-base';
import { ModalDirective } from 'ngx-bootstrap';
import * as _ from 'lodash';
import { finalize } from 'rxjs/operators';

import { LazyLoadEvent } from 'primeng/components/common/lazyloadevent';
import { Paginator } from 'primeng/components/paginator/paginator';
import { Table } from 'primeng/components/table/table';
import { ApplyChangeServiceProxy } from '@shared/service-proxies/service-proxies';
import { from } from 'rxjs';

@Component({
  selector: 'OutsourcingApplyChangeList',
  templateUrl: './OutsourcingApplyChangeList.component.html',
  styleUrls: ['./OutsourcingApplyChangeList.component.css']
})
export class OutsourcingApplyChangeListComponent  extends AppComponentBase {
  @ViewChild('paginator') paginator: Paginator;
  @ViewChild('dataTable') dataTable: Table;
  @ViewChild('createOrEditModal') modal: ModalDirective;
  active = false;
  saving = false;
  filterText = '';
  _outsourcingType: number;
  constructor(injector: Injector,
    private _applyChangeServiceProxy: ApplyChangeServiceProxy
  ) {
    super(injector);
  }

  show(code: string, outsourcingType: number): void {
    console.log(outsourcingType);
    this.filterText = code;
    this._outsourcingType = outsourcingType;
    this.active = true;
    this.modal.show();

    this.getTasks();
  }
  close(): void {
    this.active = false;
    this.modal.hide();
  }
  onShown(): void {

  }
  getTasks(event?: LazyLoadEvent) {
    if (this.primengTableHelper.shouldResetPaging(event)) {
      this.paginator.changePage(0);
      return;
    }
    this.primengTableHelper.showLoadingIndicator();
    this._applyChangeServiceProxy.getOutsourcingApplyChangeListByOutsourcingCode(
      this.filterText,
      '',
      10,
      0
    )
      .pipe(finalize(() => this.primengTableHelper.hideLoadingIndicator()))
      .subscribe(result => {
        console.log(result);
        this.primengTableHelper.totalRecordsCount = result.totalCount;
        this.primengTableHelper.records = result.items;
        this.primengTableHelper.hideLoadingIndicator();
      });
  }
}
